package com.dw.etl.core.controller;

import com.dw.etl.common.base.BaseException;
import com.dw.etl.common.base.BaseResult;
import com.dw.etl.common.constant.CommonResultConstant;
import com.dw.etl.common.vo.FieldAttrVO;
import com.dw.etl.common.vo.PaginationVO;
import com.dw.etl.core.api.BaseDbService;
import com.dw.etl.core.api.EtlBaseFieldService;
import com.dw.etl.core.api.EtlBaseService;
import com.dw.etl.core.api.TableOperService;
import com.dw.etl.core.bean.model.BaseDb;
import com.dw.etl.core.bean.model.EtlBase;
import com.dw.etl.core.bean.model.EtlBaseField;
import com.dw.etl.core.bean.vo.LayMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiOperation;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * etlBaseField控制器
 * Created by hozeData on 2021/05/28.
 */
@RestController
@RequestMapping("/etl/etlBaseField/")
@Api(description="开发中", tags="开发中")
@CrossOrigin(origins = "*", maxAge = 3600)
public class EtlBaseFieldController {

    @Autowired
    EtlBaseFieldService etlBaseFieldService;

    @ApiOperation(value="分页查询")
    @RequestMapping(value="findByPage", method=RequestMethod.POST)
    public BaseResult findByPage(@RequestBody EtlBaseField etlBaseField, Integer pageNum, Integer pageSize) {
        PaginationVO<EtlBaseField> pageVO = etlBaseFieldService.findPageByModel(etlBaseField, pageNum, pageSize);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="查询")
    @RequestMapping(value="find", method=RequestMethod.GET)
    public LayMsg find(Integer page, Integer limit,String idBase) {
        if(idBase==null) {
            return null;
        } else {
            EtlBaseField field = new EtlBaseField();
            field.setIdBase(idBase);
            List<EtlBaseField> pageVO = etlBaseFieldService.findByModel(field);
            return LayMsg.success().addCount(pageVO.size()).addData(pageVO);
        }
    }

    @ApiOperation(value="保存")
    @ApiImplicitParam(name="etlBaseField", value="实体", paramType="body", required=true, dataType="EtlBaseField")
    @RequestMapping(value="save", method=RequestMethod.POST)
    public BaseResult save(@RequestBody EtlBaseField etlBaseField, HttpServletRequest request)  {
        int num ;
        if(StringUtils.isEmpty(etlBaseField.getIdFld())) {
            num = etlBaseFieldService.insertSelective(etlBaseField);
        } else {
            num = etlBaseFieldService.updateByIdSelective(etlBaseField);
        }
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="保存")
    @RequestMapping(value="saveList", method=RequestMethod.POST)
    public BaseResult saveList(@RequestBody List<EtlBaseField> fieldList, HttpServletRequest request)  {
        etlBaseFieldService.save(fieldList);
        EtlBaseField field = new EtlBaseField();
        field.setIdBase(fieldList.get(0).getIdBase());
        List<EtlBaseField> pageVO = etlBaseFieldService.findByModel(field);
        return new BaseResult(CommonResultConstant.SUCCESS, pageVO);
    }

    @ApiOperation(value="详情")
    @ApiImplicitParam(name="idFld", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="getById", method=RequestMethod.GET)
    public BaseResult getById(String idFld, HttpServletRequest request) throws BaseException {
        if(StringUtils.isEmpty(idFld)){
            throw new BaseException("请选择要编辑的项");
        }
        EtlBaseField etlBaseField = etlBaseFieldService.getById(idFld);
        return new BaseResult(CommonResultConstant.SUCCESS, etlBaseField);
    }

    @ApiOperation(value="修改")
    @ApiImplicitParam(name="etlBaseField", value="实体", paramType="body", required=true, dataType="EtlBaseField")
    @RequestMapping(value="update", method=RequestMethod.POST)
    public BaseResult update(@RequestBody EtlBaseField etlBaseField, HttpServletRequest request)  {
        int num = etlBaseFieldService.updateByIdSelective(etlBaseField);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="根据主键删除")
    @ApiImplicitParam(name="idFld", value="主键", paramType="query", required=true, dataType="String")
    @RequestMapping(value="delete", method=RequestMethod.GET)
    public BaseResult delete(String idFld, HttpServletRequest request) throws BaseException {
        if(idFld==null){
            throw new BaseException("请选择要删除的项");
        }
        int num = etlBaseFieldService.deleteById(idFld);
        return new BaseResult(CommonResultConstant.SUCCESS, num);
    }

    @ApiOperation(value="自动获取")
    @RequestMapping(value="findByAuto", method=RequestMethod.GET)
    public BaseResult findByAuto(String idBase, HttpServletRequest request) throws BaseException {
        if(idBase==null) {
            return null;
        }
        EtlBaseField etlBaseField = new EtlBaseField();
        etlBaseField.setIdBase(idBase);
        List<EtlBaseField> fieldList = etlBaseFieldService.findByModel(etlBaseField);
        if(fieldList.size()>0) {
            return new BaseResult(CommonResultConstant.ERROR,"请先清空已有字段");
        }
        List<EtlBaseField> list = etlBaseFieldService.findFieldMsg(idBase);
        return new BaseResult(CommonResultConstant.SUCCESS, list);
    }



}
